<?php
namespace app\api\controller\passenger;
use app\api\logic\Gps;
use app\api\model;
use think\Config;
use think\Db;
use think\Exception;
use think\Hook;

/**
 * @name【乘客订单接口】
 * @baseurl【passenger】
 * @file【passenger/Order.php】
 */
class Order extends Base
{
    /**
     * @name【提交订单申请】
     * @desc【提交订单申请】
     * @uri【orderApply】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1#
     * car_type_id $ string $ 1 $ 车辆类型，2：优选型，3：舒适型，4：豪华型，5：小巴，6：中巴，7：大巴，8：执法车 $ 1#
     * order_type $ string $ 1 $ 订单类型 订单类型,1：公务订单，2：企业订单，3：个人订单 $ 1#
     * passenger_name $ string $ 小猪啊 $ 乘客姓名 $ 1#
     * passenger_phone $ string $ 17689 $ 乘客电话 $ 1#
     * from_addr $ string $ 石狮 $ 出发地 $ 1#
     * from_lat $ string $ 12.33 $ 出发地纬度 $ 1#
     * from_lon $ string $ 12.33 $ 出发地经度 $ 1#
     * estimate_mileage $ string $ 12.6 $ 公里数 $ 1#
     * booking_type $ string $ 1 $ 订车类型：1，订车（日常）。2，包车。3，分时租赁 $ 1#
     * appointment_time $ string $ 2017-07-17 12：05：05 $ 预约时间#
     * pay_type $ string $ 1 $ 2-单位支付 3-财政支付 4 自有用车,order_type=1#
     * approval_id $ int $ 1 $ 审核人id order_type=1时必填 #
     * approval_name $ string $ 1 $ 审批人 order_type=1时必填#
     * use_explain $ string $ 1 $ 用车说明 order_type=1#
     * use_type $ string $ 1 $ order_type=1 用车类型 1：行政执法，2：特殊公务，3：重要会议，4：机要通讯，5：视察调研，6：公务接待#
     * is_return $ string $ 1 $ booking_type=1，1-需要返程  9-无需#
     * to_addr $ string $ 晋江 $ booking_type=1，目的地#
     * to_lat $ string $ 12.12 $ booking_type=1，目的地纬度#
     * to_lon $ string $ 12.12 $ booking_type=1，目的地经度#
     * special_type $ int $ 0 $ 0 普遍 1报价订单 2行政执法 #
     * pre_meter $ string $ 12.12 $ 预计xx米 $ 1#
     * pre_time $ string $ 12.12 $ 预计时间 $ 1#
     * reorder $ int $ 0 $ 是否续单 0为否 续单请求下传原单id #
     * car_num $ int $ 1 $车辆数 #
     * passenger_num $ int $ 1 $ 乘客数 #
     * pre_fee $ string $ 12.12 $ 预计费用 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * data $ string $ $ 列表 #{"from_addr":"1","from_lon":"1","from_lat":"1","plan_time":"2018-03-20 16:30","call_time":"2018-03-22 09:27:46","order_id":"26343","order_no":"201803220927461267242426"}
     * 】
     */
    public function orderApply()
    {
        $string = 'car_type_id|0#车型@require|number/order_type#订单类型@require|number/special_type|0/';
        $string.= 'passenger_name#乘客姓名@require|chsDash/passenger_phone#乘客电话@require|number|length:11/';
        $string.= 'booking_type|1#预定类型@require|number/is_return#返程@number/reorder|0#续单@number/pay_type|1#支付类型@require|number/';
        $string.= 'car_num|1#车数量@require/passenger_num|1#乘客数量@require';
        $data   = $this->getBatchParam($string);
        $data['area_id']        = $this->passengerInfo['area_id'];
        $data['passenger_id']   = $this->passengerInfo['passenger_id'];
        $data['orderer']        = $this->passengerInfo['passenger_name'];
        $data['company_id']     = $this->passengerInfo['company_id'];
        $string                 = 'from_addr#出发地@require/from_lon#出发经度@require|float/from_lat#出发纬度@require|float/appointment_time|-|plan_time';
        $gpsData                = $this->getBatchParam($string);
        $tempTime = strtotime($gpsData['plan_time']);
        $gpsData['plan_time']   =  $tempTime && $tempTime > time() ? $gpsData['plan_time'] : date('Y-m-d H:i:s');
        $gpsData['call_time']   = date('Y-m-d H:i:s');
        $string                 = 'pre_meter|0#require|float/pre_time|0#require|float/pre_fee|0#require|float';
        $feeData                = $this->getBatchParam($string);
        $data['order_type'] == 3 && $data['order_state'] = 4;

        // 客户端传值单位不统一，此处做换算处理
        $speed = $feeData['pre_time'] > 0 ? ($feeData['pre_meter'] / $feeData['pre_time']) * 3.6 : 0;
        if(!$speed) {
            // 时间为0，默认一小时
            $feeData['pre_time'] = 3600;
        }elseif($speed > 10000) {
            // 速度大于10000公里/小时，时间从小时转换为秒
            $feeData['pre_time'] = $feeData['pre_time'] * 60 * 60;
        }elseif($speed > 1000) {
            // 速度大于1000公里/小时，时间从分钟转换为秒
            $feeData['pre_time'] = $feeData['pre_time'] * 60;
        }
        // 自有用车费用0
        if($data['pay_type']==4){
            $feeData['special_pay'] = 2;
        }

        // 存在个人未支付订单 已派车订单，无法在使用个人订单
        if($data['order_type'] == 1){
            $data['order_state'] = 1;
            $data['apply_time']  = date('Y-m-d H:i:s', time());       //申请时间
            $string = 'pay_type|1#支付类型@number/approval_id#审核人ID@require|number/approval_name|-|approval#审核人@require|chsDash/';
            $string.= 'use_explain#用车说明@require/use_type#用车类型@number';
            $data1  = $this->getBatchParam($string);
            $data   = array_merge($data,$data1);
            $data['use_type'] = $data['pay_type'] == 2 ? 0 : $data['use_type'];
            // 行政执法时候订单计费为0
            if($data['use_type']==1 && !isset($feeData['special_pay'])){
                $feeData['special_pay'] = 1;
            }
        }
        if($data['booking_type']==1){
            $string   = 'to_addr#目的地@require/to_lon#目的经度@require|float/to_lat#目的纬度@require|float';
            $gpsData1 = $this->getBatchParam($string);
            $gpsData  = array_merge($gpsData,$gpsData1);
        }
        if(!($data['booking_type'] == 1 && $data['order_type'] == 1)){
            unset($data['is_return']);
        }
        list($code, $msg)   = (new \app\api\logic\Order())->createOrder($data, $gpsData, $feeData);
        return $this->retAll($code, $msg);
    }

    /**
     * @name【待审批、审批历史】
     * @desc【待审批、审批历史】
     * @uri【approval_auth_person】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1#
     * order_type $ string $ 1 $ 订单类型,1：公务订单，2：企业订单，3：个人订单 $ 1#
     * order_state $ string $ 1 $ 订单状态，1：待审批，2：已批准，3：被驳回， $ 1#
     * role_id $ string $ 1 $ 乘客角色ID $ 1 #
     * page $ int $ 1 $ 页数 $1 #
     * num $ int $ 10 $ 分页大小 $1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息
     * 】
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function approval_auth_person()
    {
        $where['or.approval_id'] = $this->passengerInfo['passenger_id'];//$p['approval_name'];
        $where['or.order_type']  = input('order_type',0);
        $order_state             = input('order_state', 0);
        $page = input('page',1);
        $num  = input('num',10);
        $stateArr                = [1,['in', '2,4,5,6,7,8,9,10,11'],3];
        $where['or.order_state'] = $stateArr[$order_state - 1];//$p['order_state'];
        $field = 'or.passenger_name,or.passenger_phone,or.car_type_id,or.order_no,or.order_time,or.approval,or.reorder,';
        $field.= 'pr.name apply_name,pr.phone apply_phone,';
        $field.= 'or.order_type,or.is_return,or.use_type,or.use_explain,or.booking_type,or.appointment_time,or.pay_type,';
        $field.= 'ogs.from_addr,ogs.to_addr,ofe.pre_meter,ofe.pre_fee,ct.car_type,ct.chartered_hour,ct.chartered_charge,';
        $field.= 'cu.quota,cu.unit_quota,cu.used_quota,or.order_id,or.reject_note,ogs.call_time,ogs.plan_time,or.approval_time';
        $info  = Db::name('order')->alias('or')
            ->join('passenger pr', 'or.passenger_id = pr.passenger_id')
            ->join('car_type ct', 'or.car_type_id = ct.car_type_id')
            ->join('company cu', 'pr.company_id = cu.company_id')
            ->join('order_gps ogs', 'or.order_id = ogs.order_id')
            ->join('order_fee ofe', 'or.order_id = ofe.order_id')
            ->field($field)
            ->whereNull('or.delete_time')
            ->where($where)
            ->order('apply_time desc')
            ->page($page,$num)
            ->select();
        $count = Db::name('order')->alias('or')
            ->join('passenger pr', 'or.passenger_id = pr.passenger_id')
            ->join('car_type ct', 'or.car_type_id = ct.car_type_id')
            ->join('company cu', 'pr.company_id = cu.company_id')
            ->join('order_gps ogs', 'or.order_id = ogs.order_id')
            ->join('order_fee ofe', 'or.order_id = ofe.order_id')
            ->field('or.order_id')
            ->whereNull('or.delete_time')
            ->where($where)
            ->count();
        foreach ($info as $k => $val) {
            $info[$k]['user_type'] = config('base.use_types')[$val['use_type']];
        }
        if ($info) {
            return $this->retSucc(['data'=>$info,'count'=>$count]);
        } else {
            return $this->retError('暂无审批订单');
        }
    }

    /**
     * @name【等待派车(点击用车)】
     * @desc【等待派车(点击用车)】
     * @uri【wait_send_car】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1#
     * order_id $ int $ 1 $ 订单id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息
     * 】
     */
    public function wait_send_car()
    {
        $oid  = input('order_id',0);
        $osn  = input('order_no','');
        $where['order_state'] = 2;
        $oid > 0     && $where['order_id'] = $oid;
        !empty($osn) && $where['order_no'] = $osn;
        $orderGps = Db::name('order_gps')->where('order_id',$oid)->value('plan_time');
        if(strtotime($orderGps) < date('Y-m-d H:i:s',time()-24*60*60)){
            return $this->retError(135);
        }
        $order = Db::name('order')->where($where)->find();
        if($order['special_type']==1){
            $data['order_state'] = 9;//报价订单直接进入待支付
        }else{
            $data['order_state'] = 4;
        }
        $data['click_time']  = time();
        $ret  = Db::name('order')->where($where)->update($data);
        if ($ret) {
            // 报价订单绑定支付
            $data['special_type'] == 1 && Hook::listen('sure_fee',$oid);
            return $this->retSucc();
        } else {
//            return $this->retError(404);
            return $this->retSucc();
        }
    }

    /**
     * @name【乘客取消订单】
     * @desc【乘客取消订单】
     * @uri【cancel_order】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1#
     * order_id $ int $ 1 $订单id$ 1#
     * order_no $ string $ 15198 $ 订单号 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息
     * 】
     */
    public function cancel_order()
    {
        $where['order_id'] = input('order_id');
        list($code,$msg)  = (new model\Order())->cancelOrder(input('order_id',0),input('order_sn',''),$this->passengerInfo['passenger_id'], true);
        return $this->retAll($code,$msg);
    }

    /**
     * @name【获取司机位置】
     * @desc【获取司机位置】
     * @uri【getDriverGps】
     * @method【post】
     * @param【
     *  orderid $ int $ 0 $ 订单id $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码0失败1成功#
     * msg $ string $  $ 错误信息 #
     * data $ json $$ 轨迹 $[{}]
     * 】
     */
    public function getDriverGps(){
        $oid   = input('orderid',0);
        $order = \app\api\model\Order::get($oid);
        $data['lat'] = $order->driver->lat;
        $data['lon'] = $order->driver->lon;
        return $this->retSucc($data);
    }

    /**
     * @name【获取订单轨迹】
     * @desc【获取订单轨迹】
     * @uri【getOrderTrack】
     * @method【post】
     * @param【
     * orderid $ int $ 0 $ 订单id $ 1 #
     * stime   $ datetime $ 2017-1-1 00:00:00 $ 开始时间 #
     * etime   $ datetime $ 2017-1-1 00:00:00 $ 结束时间
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码0失败1成功#
     * msg $ string $  $ 错误信息 #
     * lon $ string $  $ 经度 #
     * lat $ string $  $ 纬度
     * 】
     */
    public function getOrderTrack(){
        $oid          = input('orderid',0);
        $order        = \app\api\model\Order::get($oid);

        $driver_id    = $order ? $order->driver_id : 0;

//        $driverTime   = $order->ordergps ? $order->ordergps->driver_time : date('Y-m-d H:i:s');
//        $stime        = input('stime',$driverTime);
//        $etime        = input('etime',date('Y-m-d H:i:s'));

        $stime = $order ? $order->ordergps->getAttr('go_time') : date('Y-m-d H:i:s');
        $etime = $order ? $order->ordergps->getAttr('arrival_time') : date('Y-m-d H:i:s');
        
        $track        = (new Gps())->getTrack($driver_id,$stime,$etime);
        $data['data'] = $track[3];
        return $this->retSucc($data);
    }

    /**
     * @name【获取订单详情-弃用】
     * @desc【获取订单详情-弃用】
     * @uri【getOrderDetail】
     * @method【post】
     * @param【
     *  orderid $ int $ 0 $ 订单id $ 1 #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码0失败1成功#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function getOrderDetail(){
        $oid                  = input('orderid',0);
        $order                = \app\api\model\Order::with('ordergps,orderfee,driver,passenger')->find($oid);
        $data['data']         = $order;
        return $this->retSucc($data);
    }

    /**
     * @name【订单费用】
     * @desc【订单费用】
     * @uri【getOrderFee】
     * @method【post】
     * @param【
     * ptoken $ string $ abcd $ 校验码 $ 1 #
     * orderid $ int $ 5 $订单id$ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息 #
     * data $ json $ $ 费用明细 $ {"start_fee":"9.00","travel_fee":"9.66","meter":"85.57","use_time":78,"time_fee":"0.00","low_time":0,"low_fee":"0.00","remote_meter":"0.00","remote_fee":"0.00","wait_time":4,"wait_fee":"0.00","driver_money":"110.13","toll":"0.00","park_fee":"0.00","other_fee":"0.00","high_fee":"100.00"}
     * 】
     */
    public function getOrderFee(){
        $oid         = input('orderid');
        $orderFee    = (new model\OrderFee())->getPassengerFee($oid);
        $data['data']= $orderFee;
        return $this->retSucc($data);
    }

    /**
     * @name【订单列表、详情-for_order】
     * @desc【订单列表、详情】
     * @uri【listOrder】
     * @method【post】
     * @param【
     * ptoken $ string $ jjcx.passenger.5d7ef8d80db071600f0ba7df5572e63a@1518169128 $ 校验参数 $ 1#
     * orderid $ int $ 1 $订单id #
     * order_state $ string $ 1,2,4,5,6,7,8,9,10,11 $ 状态字符串 #
     * order_type $ string $ 1,2,3 $ 订单类型 #
     * page $ int $ 1 $页数 #
     * pagenum $ int $ 10 $ 记录数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * count $ int $ $ 总记录数#
     * data $ string $ $ 列表$[{"order_id":26258,"order_no":"201803201942016819389716","order_time":"2018-03-20 19:42:01","order_state":11,"from_addr":"仙岳路","to_addr":"嘉盛豪园(莲前东路)","is_return":"9","evaluate":"非常棒，师傅人很帅","star":"4.8","booking_type":"1","order_type":"1","use_type":"4","car_type":"3","chartered_hour":0,"approval_name":"小黄","appointment_time":"2018-03-20 19:42:00","approval_phone":"15060816655","passenger_name":"小黄","passenger_phone":"15060816655","from_lon":"118.18100500","from_lat":"24.50852900","to_lon":"118.15485000","to_lat":"24.47852200","go_time":"2018-03-20 19:49:00","name":"帅哥","phone":"13959203611","number":"闽C66688","arrival_time":"2018-03-20 19:49:05","score":"98.60","rate":"99.00","colour":"其它","brand_name":"玛莎拉蒂 GranSport(进口)","order_count":99,"head_img":"upload\/driver\/img\/head_img\/20180317\/b3df26863b26ec32c850861caad8dfa2.jpge","user_type":"机要通讯"}]
     * 】
     */
    public function listOrder()
    {
        $oid        = input('orderid');
        $page       = input('page',1);
        $pagenum    = input('pagenum',10);
        $orderType  = input('order_type');
        $orderType && $where['order_type'] = ['in',$orderType];
        $orderState = input('order_state','1,2,4,5,6,7,8,9,10,11,21,22,23');
        $stateArr   = explode(',',$orderState);
        $where['or.passenger_id'] = $this->passengerInfo['passenger_id'];
        $oid >0 && $where['or.order_id'] = $oid;
        $prop   = $this->passengerInfo['prop'] ? $this->passengerInfo['prop'] : 0;
        $field  = 'or.order_id,or.order_no,or.order_time,or.order_state,ogs.from_addr,ogs.to_addr,or.is_return,or.evaluate,or.star order_star,';
        $field .= 'or.booking_type,or.order_type,or.use_type,ct.car_type,ct.chartered_hour,or.approval AS approval_name,or.reorder,or.pay_type';
        $field .= ',or.appointment_time,pr.phone AS approval_phone,or.passenger_name,or.passenger_phone,ogs.from_lon,';
        $field .= 'ogs.from_lat,ogs.to_lon,ogs.to_lat,ogs.go_time,dr.name,dr.phone,car.number,ogs.arrival_time,ogs.plan_time';
        $field .= ',ifnull(dr.star,5) star,ifnull(dr.score,100) score,ifnull(dr.rate,100) rate,or.reject_note,or.evaluate_tag';
        $field .= ',car.colour,car.brand_name,ifnull(dr.order_count,0) order_count,dr.head_img,cu.law_type,or.click_time,or.car_num,or.passenger_num';
        $field .= ',fee.pre_fee,or.approval_time,or.use_explain,or.approval_id,or.car_type_id,'.$prop.' as prop';
        $field .= ',fee.passenger_money ';
        try{
            $info   = Db::name('order')->alias('or')
                //车辆类型
                ->join('car_type ct', 'or.car_type_id = ct.car_type_id', 'left')
                //GPS
                ->join('order_gps ogs', 'or.order_id = ogs.order_id', 'left')
                ->join('passenger pr', 'or.approval_id = pr.passenger_id', 'left')
                ->join('order_fee fee','or.order_id = fee.order_id','left')
                ->join('driver dr', 'or.driver_id = dr.driver_id', 'left')
                ->join('car', 'car.car_id = or.car_id', 'left')
                ->join('company cu','or.company_id = cu.company_id','left')
                ->order('order_time desc')
                ->whereNull('or.delete_time')
                ->where($where)->whereIn('or.order_state', $stateArr)
                ->field($field)
                ->page($page,$pagenum)
                ->select();
        }catch (Exception $e){
            return $this->retError('数据异常');
        }
        $count   = Db::name('order')->alias('or')
            ->join('car_type ct', 'or.car_type_id = ct.car_type_id', 'left')
            ->join('order_gps ogs', 'or.order_id = ogs.order_id', 'left')
            ->join('passenger pr', 'or.approval = pr.name', 'left')
            ->join('driver dr', 'or.driver_id = dr.driver_id', 'left')
            ->join('car', 'car.car_id = or.car_id', 'left')
            ->whereNull('or.delete_time')
            ->order('order_time desc')
            ->where($where)->whereIn('or.order_state', $orderState)
            ->field($field)
            ->count();

        $webUrl = Config::get('base.web_url');
        foreach ($info as $k => $val) {
            $info[$k]['user_type'] = config('base.use_types')[$val['use_type']];
            $info[$k]['head_img'] = $info[$k]['head_img'] ? $webUrl . $info[$k]['head_img'] : '';
        }

        $string = 'order_id|订单id,order_no|订单号,order_time|下单时间,order_state|订单状态,from_addr|出发地,to_addr|目的地,is_return|是否返程';
        $string.= ',evaluate|评价,star|评星,booking_type|包车类型1普通2包车,order_type|订单类型,use_type|用车类型,car_type|车型,chartered_hour|包车小时';
        $string.= ',approval_name|审核人,appointment_time|出发时间,approval_phone|审核人电话,passenger_name|乘客姓名,passenger_phone|乘客电话';
        $string.= ',from_lon|出发地经度,from_lat|出发地纬度,to_lon|目的经度,to_lat|目的纬度,go_time|出发时间,name|司机姓名,phone|司机电话';
        $string.= ',number|车牌,arrival_time|到达时间,score|司机评分,rate|接单率,colour|车颜色,brand_name|车品牌,order_count|接单数';
        $string.= ',head_img|头像地址';
        $desc   = analyzeDesc($string);
        return $this->retSucc(['count'=>$count,'data'=>$info,'description'=>$desc]);
    }

    /**
     * @name【历史订单-公务\个人】
     * @desc【历史订单-公务\个人】
     * @uri【passenger_opration_order】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1#
     * passenger_id $ int $ 1 $ 用户乘客ID $ 1#
     * order_type $ string $ 1 $ 订单类型,1：公务订单，2：企业订单，3：个人订单 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败
     * 】
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function passenger_opration_order()
    {
        $p = input('post.');
        $where['or.passenger_id'] = $p['passenger_id'];
        //订单类型,1：公务订单，2：企业订单，3：个人订单
        $where['or.order_type'] = $p['order_type'];
        if ($p['order_type'] == '1') {
            $info = Db::name('order')->alias('or')
                //关联车辆类型表
                ->join('car_type ct', 'or.car_type_id = ct.car_type_id', 'left')
                //GPS
                ->join('order_gps ogs', 'or.order_id = ogs.order_id', 'left')
                //乘车人
                ->join('passenger pr', 'or.approval = pr.name', 'left')
                ->where($where)->whereIn('or.order_state', [3, 11, 12, 13, 14])
                ->whereNull('or.delete_time')
                ->order('order_time desc')
                ->field('or.order_time,or.order_state,or.use_type,or.booking_type,or.approval AS approval_name,or.pay_type,
                ogs.from_addr,ogs.to_addr,pr.phone AS approval_phone,ct.car_type,ct.chartered_hour,or.order_id,or.reject_note')
                ->select();
            foreach ($info as $k => $val) {
                $info[$k]['user_type'] = config('base.use_types')[$val['use_type']];
            }
        } elseif ($p['order_type'] == '3') {
            $info = Db::name('order')->alias('or')
                //关联车辆类型表
                ->join('car_type ct', 'or.car_type_id = ct.car_type_id', 'left')
                //GPS
                ->join('order_gps ogs', 'or.order_id = ogs.order_id', 'left')
                ->whereNull('or.delete_time')
                ->where($where)->whereIn('or.order_state', [3, 11, 12, 13, 14])
                ->order('order_time desc')
                ->field('or.order_time,or.order_state,or.booking_type,ogs.from_addr,ogs.to_addr,ct.car_type,ct.chartered_hour,or.order_id,or.reject_note')
                ->select();
        }
        if ($info) {
            return $this->retSucc(['data'=>$info]);
        } else {
            return $this->retError('暂无历史订单！');
        }
    }

    /**
     * @name【单位内审批员】
     * @desc【单位内审批员】
     * @uri【change_part_approval】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * data $ string $ $ 列表 $ [{"approval_name":"小黄","approval_phone":"15060816655"},{"approval_name":"小白","approval_phone":"15060816655"}]
     * 】
     */
    public function change_part_approval()
    {
        $company_id          = Db::name('passenger')->where('passenger_id', $this->passengerInfo['passenger_id'])->value('company_id');
        $where['is_check']   = 1;
        $where['company_id'] = $company_id;
        $field               = 'name as approval_name,phone as approval_phone,passenger_id approval_id';
        $list = Db::name('passenger')->where($where)->field($field)->select();
        if ($list) {
            return $this->retSucc(['data'=>$list]);
        } else {
            return $this->retError('暂无审批员');
        }
    }

    /**
     * @name【更换审核人】
     * @desc【更换审核人】
     * @uri【changeApproval】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1 #
     * order_id $ int $ 1 $ 订单id $ 1 #
     * approval $ string $ 张三 $ 审核人 $ 1 #
     * approval_id $ int $ 1 $ 审核人ID $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息
     * 】
     */
    public function changeApproval(){
        $string = 'order_id#订单@require|number/approval_id#审批人id@require|number/';
        $string.= 'approval#审批人姓名@require|chsDash';
        $data   = $this->getBatchParam($string);
        $data['pid'] = $this->passengerInfo['passenger_id'];
        list($code,$msg) = (new model\Order())->changeApproval($data);
        return $this->retAll($code,$msg);
    }

    /**
     * @name【取消审核】
     * @desc【取消审核】
     * @uri【cancelApproval】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1 #
     * order_id $ int $ 1 $ 订单ID $1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息
     * 】
     */
    public function cancelApproval()
    {
        $oid   = input('order_id',0);
        $order = Db::name('order')->where('order_id',$oid)->find();
        if($order['order_state']<2 || $order['order_state']>4){
            return $this->retError('订单无法撤销');
        }
        $data = ['order_state'=>1];
        $ret  = Db::name('order')->where('order_id',$oid)->update($data);
        if ($ret>0) {
            return $this->retSucc();
        } else {
            return $this->retError('不能重复取消审核');
        }
    }

    /**
     * @name【轮询查订单状态】
     * @desc【轮询查订单状态】
     * @uri【circleQueryOrder】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1 #
     * orderid $ int $ 1 $ 订单ID $1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息
     * 】
     */
    public function circleQueryOrder(){
        // $pid        = $this->passengerInfo['passenger_id'];
        $oid        = input('orderid',0);
        $data['order_state'] = Db::name('order')->where('order_id',$oid)->value('order_state');
        return $this->retSucc($data);
    }

    /**
     * @name【确认报价】
     * @desc【确认报价】
     * @uri【sureBusPrice】
     * @method【post】
     * @param【
     * ptoken $ string $ token $ 校验码 $ 1 #
     * order_id $ int $ 1 $ 订单ID $1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息
     * 】
     */
    public function sureBusPrice(){
        $oid = input('order_id');
        list($code,$msg) = (new model\Order())->sureBusPrice($oid);
        return $this->retAll($code,$msg);
    }
}